package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.expression.ExpressionInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/11/2 12:58
 */
public class ExpressionInfoDaoImpl extends TempDao {
    private static String getSql = "select * from `t_p_expression`";
    public Map<Integer, ExpressionInfo> getExpressionInfoMap() {
        Map<Integer, ExpressionInfo> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getSql);
            rs = call.executeQuery();
            while (rs.next()) {
                ExpressionInfo expressionInfo = new ExpressionInfo();
                expressionInfo.setId(rs.getInt("id"));
                expressionInfo.setItemId(rs.getInt("itemId"));
                expressionInfo.setFree(rs.getBoolean("isFree"));
                expressionInfo.setType(rs.getInt("type"));
                expressionInfo.setNum(rs.getInt("num"));
                map.put(expressionInfo.getId(), expressionInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
